{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1699a561-c44b-410f-a01f-bef407cca093",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "好，用户问了“你好？”，然后得到了一个回复说“让我们一步一步地思考吧。”。这看起来像是用户可能在测试系统或者开始对话。\n",
      "\n",
      "首先，我应该理解用户的意图是什么。可能是想确认系统是否在线，或者准备开始一个问题的讨论。\n",
      "\n",
      "接下来，分析一下回复：“Let's think step by step.” 看起来是鼓励深入探讨问题，但用户最初只是简单的问候。\n",
      "\n",
      "或许用户希望得到更具体的帮助，比如解答某个问题或提供建议。\n",
      "\n",
      "这时候，我应该主动提出进一步的问题，询问用户具体需要什么帮助，这样可以更好地满足他们的需求。\n",
      "\n",
      "所以，合适的回应应该是友好且开放的，邀请用户提供更多信息或者问题。\n",
      "</think>\n",
      "\n",
      "你好！有什么我可以帮助你的吗？无论是学习、工作还是生活中的问题，都可以告诉我哦！\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_ollama.llms import OllamaLLM\n",
    "\n",
    "template = \"\"\"Question: {question}\n",
    "\n",
    "Answer: Let's think step by step.\"\"\"\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "model = OllamaLLM(model=\"deepseek-r1:32b\", base_url=\"http://julyperla.asuscomm.com:11434/\")\n",
    "\n",
    "chain = prompt | model\n",
    "output = chain.invoke({\"question\": \"你好?\"})\n",
    "\n",
    "output = re.sub(r'<think>.*?</think>', '', output)\n",
    "\n",
    "print(output)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb32a837-63dd-4d6d-882e-95e5a6e3e9eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "“”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5f51c04e-07b5-4394-a8ef-4b1ed3704212",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[#################################] 100% de421.bsp\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "水星的位置: 右_ascension=22.609105265337583 小时, 倾斜角=-10.37775152647042 度\n"
     ]
    }
   ],
   "source": [
    "from skyfield.api import Loader, utc\n",
    "from skyfield.positionlib import Geocentric\n",
    "from skyfield.starlib import Star\n",
    "from skyfield.data import hipparcos\n",
    "import datetime\n",
    "\n",
    "# 加载天文数据\n",
    "load = Loader('~/skyfield-data')\n",
    "ts = load.timescale()\n",
    "eph = load('de421.bsp')  # 这是一个小型的行星历表文件\n",
    "\n",
    "# 获取当前时间\n",
    "t = ts.now()\n",
    "\n",
    "# 计算水星的位置\n",
    "mercury = eph['mercury']\n",
    "astrometric = eph['earth'].at(t).observe(mercury)\n",
    "ra, dec, distance = astrometric.radec()\n",
    "\n",
    "print(f\"水星的位置: 右_ascension={ra.hours} 小时, 倾斜角={dec.degrees} 度\")\n",
    "\n",
    "# 注意：上面的代码给出了水星在天空中的位置（赤经和赤纬），但并没有直接给出它位于哪个星座。\n",
    "# // 要确定星座，可以使用额外的星座边界数据或者在线API，比如通过调用Stellarium Web API等服务获取更精确的信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e593c153-c320-49e2-9347-22238dfc66b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "水星当前位于星座: Aquarius\n"
     ]
    }
   ],
   "source": [
    "from astropy.coordinates import SkyCoord, get_constellation\n",
    "import astropy.units as u\n",
    "\n",
    "# 根据给定的右升和倾角创建一个SkyCoord对象\n",
    "coord = SkyCoord(ra=22.609105265337583*u.hourangle, dec=-10.37775152647042*u.deg)\n",
    "\n",
    "# 使用get_constellation函数获取星座名称\n",
    "constellation = get_constellation(coord)\n",
    "\n",
    "print(f\"水星当前位于星座: {constellation}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "51468edf-c498-4964-a484-f028b206d4fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "水星的位置: 右_ascension=7.242084716028944 小时, 倾斜角=26.134320538455874 度\n",
      "水星当前位于星座: Gemini\n"
     ]
    }
   ],
   "source": [
    "from skyfield.api import Loader, utc\n",
    "from astropy.coordinates import SkyCoord\n",
    "import astropy.units as u\n",
    "from skyfield.positionlib import Geocentric\n",
    "\n",
    "# 加载天文数据\n",
    "load = Loader('~/skyfield-data')\n",
    "ts = load.timescale()\n",
    "eph = load('de421.bsp')  # 这是一个小型的行星历表文件\n",
    "\n",
    "# 获取当前时间或指定时间\n",
    "t = ts.now()  # 当前时间\n",
    "# 如果需要指定时间，可以使用如下方式：\n",
    "# t = ts.utc(2025, 2, 18)  # 指定日期\n",
    "\n",
    "# 计算水星的位置\n",
    "mercury = eph['mars']\n",
    "astrometric = eph['earth'].at(t).observe(mercury)\n",
    "ra, dec, distance = astrometric.radec()\n",
    "\n",
    "print(f\"水星的位置: 右_ascension={ra.hours} 小时, 倾斜角={dec.degrees} 度\")\n",
    "\n",
    "# 使用 Astropy 转换为 SkyCoord 对象并查找星座\n",
    "coord = SkyCoord(ra=ra.hours*u.hourangle, dec=dec.degrees*u.deg)\n",
    "constellation = coord.get_constellation()\n",
    "\n",
    "print(f\"水星当前位于星座: {constellation}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "86de023f-cd85-4934-aeff-29d5c42851b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "345.91519036180466\n",
      "0\n",
      "57.501840144046035\n",
      "1\n",
      "82.58388500131912\n",
      "2\n",
      "103.57337430268987\n",
      "3\n",
      "125.53742566129375\n",
      "4\n",
      "153.0150210964705\n",
      "5\n",
      "191.31648158343705\n",
      "6\n",
      "237.50184014404604\n",
      "7\n",
      "262.5838850013191\n",
      "8\n",
      "283.5733743026899\n",
      "9\n",
      "305.53742566129375\n",
      "10\n",
      "333.0150210964705\n",
      "11\n",
      "11.316481583437067\n",
      "水星位于第12宫\n"
     ]
    }
   ],
   "source": [
    "import swisseph as swe\n",
    "from datetime import datetime\n",
    "import pytz\n",
    "\n",
    "# 设置时区和时间\n",
    "timezone = pytz.timezone('UTC')  # 根据需要更改时区\n",
    "dt = datetime(2025, 2, 23, 2, 33, tzinfo=timezone)\n",
    "# dt = datetime.now()# 当前时间或指定时间\n",
    "\n",
    "# 初始化瑞士星历表\n",
    "swe.set_ephe_path(None)  # 使用默认路径\n",
    "\n",
    "# 转换为Julian日\n",
    "julian_day = swe.julday(dt.year, dt.month, dt.day, dt.hour + dt.minute / 60.0)\n",
    "\n",
    "# 获取地理坐标（例如北京）\n",
    "lat, lon, alt = 39.9042, 116.4074, 0  # 北京的经纬度\n",
    "\n",
    "# 计算宫位\n",
    "houses, ascmc = swe.houses(julian_day, lat, lon, b'P')  # 'P'表示Placidus分宫制\n",
    "\n",
    "# 计算水星位置\n",
    "mercury_pos = swe.calc_ut(julian_day, swe.MERCURY)[0]\n",
    "mercury_long = mercury_pos[0]  # 黄经\n",
    "print(mercury_long)\n",
    "\n",
    "# 找到水星所在的宫位\n",
    "house_index = -1\n",
    "for i in range(12):\n",
    "    print(i)\n",
    "    print(houses[i])\n",
    "    if i == 11:\n",
    "        house_index = 12\n",
    "        break\n",
    "    else:\n",
    "        if houses[i] <= mercury_long < houses[(i + 1) % 12]:\n",
    "            house_index = i + 1\n",
    "            break\n",
    "\n",
    "if house_index != -1:\n",
    "    print(f\"水星位于第{house_index}宫\")\n",
    "else:\n",
    "    print(\"未能确定水星所在的宫位\")\n",
    "\n",
    "# 清理资源\n",
    "swe.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "deac7d74-5aea-41f6-b9ee-4e884ae038cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cancer\n"
     ]
    }
   ],
   "source": [
    "# def get_zodiac_sign(day, month):\n",
    "#     # 定义星座的边界日期和对应的名字\n",
    "    \n",
    "\n",
    "def birthday_to_Astro(birthday_str):\n",
    "    # 确保输入是字符串形式的8位数字\n",
    "    if len(birthday_str) != 8 or not birthday_str.isdigit():\n",
    "        print(\"Invalid input\")\n",
    "        return\n",
    "    \n",
    "    year, month, day = int(birthday_str[:4]), int(birthday_str[4:6]), int(birthday_str[6:])\n",
    "\n",
    "    zodiac_signs = [\n",
    "        ((1, 19), \"Capricorn\"), ((1, 20), \"Aquarius\"), ((2, 18), \"Pisces\"),\n",
    "        ((3, 20), \"Aries\"), ((4, 19), \"Taurus\"), ((5, 20), \"Gemini\"),\n",
    "        ((6, 20), \"Cancer\"), ((7, 22), \"Leo\"), ((8, 22), \"Virgo\"),\n",
    "        ((9, 22), \"Libra\"), ((10, 22), \"Scorpio\"), ((11, 21), \"Sagittarius\"),\n",
    "        ((12, 21), \"Capricorn\")\n",
    "    ]\n",
    "    for (m, d), sign in zodiac_signs:\n",
    "        if (month == m and day <= d) or (month == 12 and day >= 22):\n",
    "            return sign\n",
    "\n",
    "# 测试\n",
    "birthday = '20030608'  # 注意这里应该是字符串\n",
    "print(birthday_to_zodiac(birthday))  # 输出应该是 \"Gemini\" 或者对应日期的星座"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c7ab42f7-30b5-4001-82b4-6a6a1b7421df",
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'swe_calc_ut' from 'swisseph' (C:\\ProgramData\\miniconda3\\envs\\langchain_env\\lib\\site-packages\\swisseph.cp310-win_amd64.pyd)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[26], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mswisseph\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m swe_calc_ut, swe_houses_ut, set_ephe_path\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mdatetime\u001b[39;00m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;66;03m# 设置瑞士史诗数据文件路径，如果它们不在默认位置的话。\u001b[39;00m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;66;03m# 这里假设你已经有了ephe路径，并且指向了正确的目录。\u001b[39;00m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'swe_calc_ut' from 'swisseph' (C:\\ProgramData\\miniconda3\\envs\\langchain_env\\lib\\site-packages\\swisseph.cp310-win_amd64.pyd)"
     ]
    }
   ],
   "source": [
    "from swisseph import swe_calc_ut, swe_houses_ut, set_ephe_path\n",
    "import datetime\n",
    "\n",
    "# 设置瑞士史诗数据文件路径，如果它们不在默认位置的话。\n",
    "# 这里假设你已经有了ephe路径，并且指向了正确的目录。\n",
    "set_ephe_path(None)\n",
    "\n",
    "def get_planet_house(date_str, planet_name, longitude, latitude):\n",
    "    # 解析输入的日期字符串为datetime对象\n",
    "    year = int(date_str[:4])\n",
    "    month = int(date_str[4:6])\n",
    "    day = int(date_str[6:])\n",
    "    date_time = datetime.datetime(year, month, day)\n",
    "    \n",
    "    # 将datetime转换为Julian Day Number (UT)\n",
    "    jd_et = swe_calc_ut(date_time.timestamp(), 0, flag=1)[0]\n",
    "    \n",
    "    # 计算宫位\n",
    "    houses, ascmc = swe_houses_ut(jd_et, latitude, longitude, b'tp')\n",
    "    \n",
    "    # 获取行星位置\n",
    "    planets = {\n",
    "        'sun': 0,\n",
    "        'moon': 1,\n",
    "        'mercury': 2,\n",
    "        'venus': 3,\n",
    "        'mars': 4,\n",
    "        'jupiter': 5,\n",
    "        'saturn': 6,\n",
    "        'uranus': 7,\n",
    "        'neptune': 8,\n",
    "        'pluto': 9,\n",
    "    }\n",
    "    \n",
    "    if planet_name.lower() not in planets:\n",
    "        return \"未知的行星名称\"\n",
    "    \n",
    "    planet_id = planets[planet_name.lower()]\n",
    "    planet_pos = swe_calc_ut(jd_et, planet_id, flag=1)[0]\n",
    "    \n",
    "    # 找出行星所在的宫位\n",
    "    for i in range(12):\n",
    "        if houses[i] <= planet_pos < houses[(i+1)%12]:\n",
    "            return f\"{planet_name.capitalize()}位于第{i + 1}宫\"\n",
    "    \n",
    "    return \"无法确定行星位置\"\n",
    "\n",
    "# 使用示例\n",
    "date_str = '20250223'\n",
    "planet_name = 'mars'\n",
    "longitude = 116.4074  # 北京经度\n",
    "latitude = 39.9042    # 北京纬度\n",
    "print(get_planet_house(date_str, planet_name, longitude, latitude))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b234efd1-fb57-4a16-b2ca-1f470240b53a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！很高兴见到你。\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 原始字符串\n",
    "text = \"<think>最后，结合所有这些因素，我可以为用户提供一个全面而个性化的星座运势分析，帮助他们更好地理解当前的状态并做出相应的调整。</think>你好！很高兴见到你。\"\n",
    "\n",
    "# 使用正则表达式移除<think>...</think>之间的内容\n",
    "result = re.sub(r'<think>.*?</think>', '', text)\n",
    "\n",
    "print(result.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8785077-bc95-458b-8a72-93288e4f0b11",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
